package View;

import client.ClientManager;
import com.mysql.jdbc.Messages;
import function.Time;
import instruction.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Callback;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

public class ChatWindow extends window {
    private Friend friend;
    private User user;
    private ListView<Message> messageList;
    private ObservableList<Message> privateMessageData = FXCollections.observableArrayList();
    private MainWindow mainWindow;
    private ClientManager clientManager;

    public ObservableList<Message> getPrivateMessageData() {
        return privateMessageData;
    }

    public Friend getFriend() {
        return friend;
    }

    public ChatWindow(MainWindow mainWindow, ClientManager clientManager, Friend friend) throws IOException {
        this.friend = friend;
        this.mainWindow = mainWindow;
        this.clientManager = clientManager;
        this.user=mainWindow.getUserData().getUser();
        root = FXMLLoader.load(getClass().getResource("/Fxml/chat.fxml"));
        Scene scene = new Scene(root, 800, 504);
        scene.setFill(Color.TRANSPARENT);
        setScene(scene);
        initStyle(StageStyle.TRANSPARENT);
        setResizable(false);
        messageList = ((ListView) $("chatList"));
        Image image=new Image("/Picture/sourceMaterial/群聊背景图.jpg",800,504,false,false);
        ((ImageView)$("backgroundImage")).setImage(image);
        setTitle("We Chat");
        InitMessage();//初始化信息呈现格式
        InitialFriend();//初始化朋友信息展示
        InitialEmoticon();//初始化表情面板
        sendMessage();//发送信息监听事件
        common();//常用语监听事件
        sendPicture();//图片发送
        sendFile();//文件发送
        ((AnchorPane)$("emoticonAnc")).setVisible(false);
        emoticon();//表情监听
        move();
        quit();
        setIcon();
        minimiser();
    }


    //退出
    @Override
    public void quit() {
        ((Button) $("exitButton")).setTooltip(new Tooltip("退出"));
        ((Button) $("exitButton")).setOnAction(event -> {
            hide();
        });

    }

    //最小化
    @Override
    public void minimiser() {
        ((Button) $("ministerButton")).setTooltip(new Tooltip("最小化"));
        ((Button) $("ministerButton")).setOnAction(event -> {
            setIconified(true);
        });
    }
    //初始化好友信息
    public void InitialFriend() {
        ((Label) $("remarkLabel")).setText(friend.getFriendRemarks());
        ((Label) $("accountLabel")).setText(friend.getMember().getAccount());
        ((Label) $("birthdayLabel")).setText(friend.getMember().getBirthday());
        ((Label) $("sexLabel")).setText(friend.getMember().getSex());
        ((Label) $("signLabel")).setText(friend.getMember().getSignature());
        ((ImageView) $("friendImage")).setImage(new Image(friend.getMember().getHead(), 50, 50, false, false));
        Circle circle = new Circle();
        circle.setRadius(25);
        circle.setCenterX(25);
        circle.setCenterY(25);
        ((ImageView) $("friendImage")).setClip(circle);
    }
    //初始化消息显示
    public void InitMessage() {
        messageList.setItems(privateMessageData);
        messageList.setCellFactory(new Callback<ListView<Message>, ListCell<Message>>() {
            @Override
            public ListCell<Message> call(ListView<Message> param) {
                ListCell<Message> listCell;
                listCell = new ListCell<Message>() {
                    @Override
                    protected void updateItem(Message item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty && item != null) {
                            HBox hBox = new HBox(10);
                            VBox vBox = new VBox(1);
                            Label label = null;
                            String type=item.getPrivateMessage().getType();
                            ImageView imageView = new ImageView();
                            if(!type.equals("file")){
                                if (type.equals("word")) {
                                    label = new Label(item.getPrivateMessage().getTime() + "\n" + item.getPrivateMessage().getContent());
                                } else if (type.equals("emoticon")) {
                                    label = new Label(item.getPrivateMessage().getTime() + "\n");
                                    imageView.setImage(new Image(item.getPrivateMessage().getContent()));
                                } else if (type.equals("picture")) {
                                    label = new Label(item.getPrivateMessage().getTime() + "\n");
                                    imageView.setImage(new Image(item.getPrivateMessage().getContent(), 80, 80, false, false));
                                }
                                label.setWrapText(true);
                                label.setFont(new Font(14));
                                if (item.getPrivateMessage().getSendAccount().equals(user.getAccount())) {
                                    ImageView iv = new ImageView(new Image(user.getHead(), 40, 40, false, false));
                                    Circle circle1 = new Circle();
                                    circle1.setRadius(20);
                                    circle1.setCenterX(20);
                                    circle1.setCenterY(20);
                                    iv.setClip(circle1);
                                    vBox.getChildren().addAll(label, imageView);
                                    hBox.getChildren().addAll(vBox, iv);
                                    hBox.setAlignment(Pos.CENTER_RIGHT);
                                } else {
                                    ImageView iv = new ImageView(new Image(friend.getMember().getHead(), 40, 40, false, false));
                                    Circle circle1 = new Circle();
                                    circle1.setRadius(20);
                                    circle1.setCenterX(20);
                                    circle1.setCenterY(20);
                                    iv.setClip(circle1);
                                    vBox.getChildren().addAll(label, imageView);
                                    hBox.getChildren().addAll(iv, vBox);
                                    hBox.setAlignment(Pos.CENTER_LEFT);
                                }
                            }else{
                                label=new Label(item.getPrivateMessage().getContent());
                                vBox.getChildren().add(label);
                                hBox.getChildren().add(vBox);
                                hBox.setAlignment(Pos.CENTER);
                            }
                            this.setGraphic(hBox);
                        } else {
                            setText(null);
                            setGraphic(null);
                        }
                    }
                };
                return listCell;
            }
        });
    }
    //初始化表情面板
    public void InitialEmoticon() {
        ((ImageView) $("1")).setImage(new Image("/Picture/emoticon/1.gif", 20, 20, false, false));
        ((ImageView) $("1")).setClip(getCircle());
        ((ImageView) $("2")).setImage(new Image("/Picture/emoticon/2.gif", 20, 20, false, false));
        ((ImageView) $("2")).setClip(getCircle());
        ((ImageView) $("3")).setImage(new Image("/Picture/emoticon/3.gif", 20, 20, false, false));
        ((ImageView) $("3")).setClip(getCircle());
        ((ImageView) $("4")).setImage(new Image("/Picture/emoticon/4.gif", 20, 20, false, false));
        ((ImageView) $("4")).setClip(getCircle());
        ((ImageView) $("5")).setImage(new Image("/Picture/emoticon/5.gif", 20, 20, false, false));
        ((ImageView) $("5")).setClip(getCircle());
        ((ImageView) $("6")).setImage(new Image("/Picture/emoticon/6.gif", 20, 20, false, false));
        ((ImageView) $("6")).setClip(getCircle());
        ((ImageView) $("7")).setImage(new Image("/Picture/emoticon/7.gif", 20, 20, false, false));
        ((ImageView) $("7")).setClip(getCircle());
        ((ImageView) $("8")).setImage(new Image("/Picture/emoticon/8.gif", 20, 20, false, false));
        ((ImageView) $("8")).setClip(getCircle());
        ((ImageView) $("9")).setImage(new Image("/Picture/emoticon/9.gif", 20, 20, false, false));
        ((ImageView) $("9")).setClip(getCircle());
        ((ImageView) $("10")).setImage(new Image("/Picture/emoticon/10.gif", 20, 20, false, false));
        ((ImageView) $("10")).setClip(getCircle());
        ((ImageView) $("11")).setImage(new Image("/Picture/emoticon/11.gif", 20, 20, false, false));
        ((ImageView) $("11")).setClip(getCircle());
        ((ImageView) $("12")).setImage(new Image("/Picture/emoticon/12.gif", 20, 20, false, false));
        ((ImageView) $("12")).setClip(getCircle());
        ((ImageView) $("13")).setImage(new Image("/Picture/emoticon/13.gif", 20, 20, false, false));
        ((ImageView) $("13")).setClip(getCircle());
        ((ImageView) $("14")).setImage(new Image("/Picture/emoticon/14.gif", 20, 20, false, false));
        ((ImageView) $("14")).setClip(getCircle());
        ((ImageView) $("15")).setImage(new Image("/Picture/emoticon/15.gif", 20, 20, false, false));
        ((ImageView) $("15")).setClip(getCircle());
        ((ImageView) $("16")).setImage(new Image("/Picture/emoticon/16.gif", 20, 20, false, false));
        ((ImageView) $("16")).setClip(getCircle());
        ((ImageView) $("17")).setImage(new Image("/Picture/emoticon/17.gif", 20, 20, false, false));
        ((ImageView) $("17")).setClip(getCircle());
        ((ImageView) $("18")).setImage(new Image("/Picture/emoticon/18.gif", 20, 20, false, false));
        ((ImageView) $("18")).setClip(getCircle());
        ((ImageView) $("19")).setImage(new Image("/Picture/emoticon/19.gif", 20, 20, false, false));
        ((ImageView) $("19")).setClip(getCircle());
        ((ImageView) $("20")).setImage(new Image("/Picture/emoticon/20.gif", 20, 20, false, false));
        ((ImageView) $("20")).setClip(getCircle());
        ((ImageView) $("21")).setImage(new Image("/Picture/emoticon/21.gif", 20, 20, false, false));
        ((ImageView) $("21")).setClip(getCircle());
        ((ImageView) $("22")).setImage(new Image("/Picture/emoticon/22.gif", 20, 20, false, false));
        ((ImageView) $("22")).setClip(getCircle());
        ((ImageView) $("23")).setImage(new Image("/Picture/emoticon/23.gif", 20, 20, false, false));
        ((ImageView) $("23")).setClip(getCircle());
        ((ImageView) $("24")).setImage(new Image("/Picture/emoticon/24.gif", 20, 20, false, false));
        ((ImageView) $("24")).setClip(getCircle());
        ((ImageView) $("25")).setImage(new Image("/Picture/emoticon/25.gif", 20, 20, false, false));
        ((ImageView) $("25")).setClip(getCircle());
        ((ImageView) $("26")).setImage(new Image("/Picture/emoticon/26.gif", 20, 20, false, false));
        ((ImageView) $("26")).setClip(getCircle());
        ((ImageView) $("27")).setImage(new Image("/Picture/emoticon/27.gif", 20, 20, false, false));
        ((ImageView) $("27")).setClip(getCircle());
        ((ImageView) $("28")).setImage(new Image("/Picture/emoticon/28.gif", 20, 20, false, false));
        ((ImageView) $("28")).setClip(getCircle());
        ((ImageView) $("29")).setImage(new Image("/Picture/emoticon/29.gif", 20, 20, false, false));
        ((ImageView) $("20")).setClip(getCircle());
        ((ImageView) $("30")).setImage(new Image("/Picture/emoticon/30.gif", 20, 20, false, false));
        ((ImageView) $("30")).setClip(getCircle());
    }
    public Circle getCircle() {
        Circle circle = new Circle();
        circle.setRadius(20);
        circle.setCenterX(20);
        circle.setCenterY(20);
        return circle;
    }
    //发送消息按钮监听
    public void sendMessage() {
        ((SplitMenuButton) $("sendButton")).setOnAction(event -> {
            String content = ((TextArea) $("inputText")).getText();
            ((TextArea) $("inputText")).setText("");
            String sendAccount = mainWindow.getUserData().getUser().getAccount();
            String receiveAccount = ((Label) $("accountLabel")).getText();
            String time = Time.getTime();
            PrivateMessage privateMessage = new PrivateMessage("word");
            privateMessage.setContent(content);
            privateMessage.setSendAccount(sendAccount);
            privateMessage.setReceiveAccount(receiveAccount);
            privateMessage.setTime(time);
            Message message=new Message("privateChat");
            message.setPrivateMessage(privateMessage);
            privateMessageData.add(message);
            clientManager.getSend().send(message);
        });
    }
    //常用语按钮监听
    public void common(){
        ((SplitMenuButton) $("sendButton")).getItems().get(0).setText("这也太秀了吧！");
        ((SplitMenuButton) $("sendButton")).getItems().get(1).setText("你好。");
        ((SplitMenuButton) $("sendButton")).getItems().get(2).setText("你好，我现在有事不在，一会儿再跟你联系");
        ((SplitMenuButton) $("sendButton")).getItems().get(3).setText("好的好的。");
        ((SplitMenuButton) $("sendButton")).getItems().get(4).setText("真的吗？");
        ((SplitMenuButton) $("sendButton")).getItems().get(0).setOnAction(actionEvent -> {
            ((TextArea)$("inputText")).setText("这也太秀了吧！");
        });
        ((SplitMenuButton) $("sendButton")).getItems().get(1).setOnAction(actionEvent -> {
            ((TextArea)$("inputText")).setText("你好。");
        });
        ((SplitMenuButton) $("sendButton")).getItems().get(2).setOnAction(actionEvent -> {
            ((TextArea)$("inputText")).setText("你好，我现在有事不在，一会儿再跟你联系");
        });
        ((SplitMenuButton) $("sendButton")).getItems().get(3).setOnAction(actionEvent -> {
            ((TextArea)$("inputText")).setText("好的好的。");
        });
        ((SplitMenuButton) $("sendButton")).getItems().get(4).setOnAction(actionEvent -> {
            ((TextArea)$("inputText")).setText("真的吗？");
        });
    }
    //发送表情监听
    public void emoticon(){
        //弹出表情框
        ((Button)$("expressionButton")).setOnAction(actionEvent ->{
            ((AnchorPane)$("emoticonAnc")).setVisible(true);
            ((ImageView) $("1")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/1.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);

            });
            ((ImageView) $("2")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/2.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("3")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/3.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("4")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/4.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("5")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/5.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("6")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/6.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("7")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/7.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("8")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/8.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("9")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/9.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("10")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/10.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("11")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/11.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("12")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/12.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("13")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/13.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("14")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/14.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("15")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/15.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("16")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/16.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("17")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/17.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("18")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/18.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("19")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/19.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("20")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/20.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("21")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/21.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("22")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/22.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("23")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/23.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("24")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/24.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("25")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/25.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("26")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/26.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("27")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/27.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("28")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/28.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("29")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/29.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });
            ((ImageView) $("30")).setOnMouseClicked(event -> {
                Message message = new Message("privateChat");
                PrivateMessage privateMessage=new PrivateMessage("emoticon");
                privateMessage.setSendAccount(user.getAccount());
                privateMessage.setReceiveAccount(friend.getMember().getAccount());
                privateMessage.setContent("/Picture/emoticon/30.gif");
                privateMessage.setTime(Time.getTime());
                message.setPrivateMessage(privateMessage);
                clientManager.getSend().send(message);
                privateMessageData.add(message);
                ((AnchorPane)$("emoticonAnc")).setVisible(false);
            });

        });
        //收起表情框
        ((Button)$("emoticonExit")).setOnAction(event->{
            ((AnchorPane)$("emoticonAnc")).setVisible(false);
        });
    }
    public void sendPicture(){
        ((Button)$("pictureButton")).setOnAction(event->{
            FileChooser fileChooser = new FileChooser();//创建一个文件选择器
            fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Photo Files", "*.jpg", "*.png", "*.bmp", "*.jpeg"));
            File file = fileChooser.showOpenDialog(new Stage());//选择文件
            if(file!=null){
                FileInputStream fis= null;
                try {
                    fis = new FileInputStream(file);
                    byte[] buf = new byte[fis.available()];
                    fis.read(buf);//图片转为二进制文件存储在字节数组中
                    fis.close();
                    PrivateMessage privateMessage=new PrivateMessage("picture");
                    privateMessage.setSendAccount(user.getAccount());
                    privateMessage.setReceiveAccount(friend.getMember().getAccount());
                    privateMessage.setTime(Time.getTime());
                    privateMessage.setPicture(buf);
                    privateMessage.setContent(file.getName());
                    Message message=new Message("privateChat");
                    message.setPrivateMessage(privateMessage);
                    clientManager.getSend().send(message);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Message message1=new Message("privateChat");
                PrivateMessage privateMessage1=new PrivateMessage("picture");
                privateMessage1.setSendAccount(user.getAccount());
                privateMessage1.setReceiveAccount(friend.getMember().getAccount());
                privateMessage1.setTime(Time.getTime());
                privateMessage1.setContent("file:///" + file.getAbsolutePath());
                message1.setPrivateMessage(privateMessage1);
                privateMessageData.add(message1);
            }
        });
    }
    public void sendFile(){
            ((Button)$("fileButton")).setOnAction(event->{
                FileChooser fileChooser = new FileChooser();
                File file = fileChooser.showOpenDialog(new Stage());
                if (file != null) {
                    try{
                        FileInputStream fis=new FileInputStream(file);
                        byte[] buf=new byte[fis.available()];
                        fis.read(buf);
                        fis.close();
                        Message message=new Message("privateChat");
                        PrivateMessage privateMessage=new PrivateMessage("file");
                        String senderAccount=user.getAccount();
                        String receiverAccount=friend.getMember().getAccount();
                        String content=file.getName();
                        String time=Time.getTime();
                        privateMessage.setSendAccount(senderAccount);
                        privateMessage.setReceiveAccount(receiverAccount);
                        privateMessage.setContent(content);
                        privateMessage.setTime(time);
                        privateMessage.setFile(buf);
                        message.setPrivateMessage(privateMessage);
                        clientManager.getSend().send(message);
                        privateMessage.setContent(content+"已发送");
                        message.setPrivateMessage(privateMessage);
                        privateMessageData.add(message);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            });
    }

}
